//
//  KSTabButtonView.m
//  Dingdong
//
//  Created by 程强 on 16/11/17.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KSTabButtonView.h"

@interface KSTabButtonView()
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *line;
@property (strong, nonatomic) UIButton *lastBtn;
@end

@implementation KSTabButtonView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:_scrollView];
        self.showIntervalLine = YES;
        self.showHoritonzalLine = YES;
        self.leading = 0;
        self.defaultSelectIndex = 0;
        self.selectedColor = kRedColor;
    }
    return self;
}
- (void)setSelectedColor:(UIColor *)selectedColor
{
    _selectedColor = selectedColor;
}
- (void)setItems:(NSArray *)items
{
    _items = items;
    
    CGFloat width = (self.bounds.size.width-2*self.leading)/items.count;
    CGFloat height = self.bounds.size.height-1;
    
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:obj forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [btn setTitleColor:RGBColor(100, 100, 100) forState:UIControlStateNormal];
        [btn setTitleColor:self.selectedColor forState:UIControlStateSelected];
        [btn setFrame:CGRectMake(idx*width+self.leading, 0, width, height)];
        btn.tag = 100+idx;
        [btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:btn];
        
        if (idx == _defaultSelectIndex) {
            btn.selected = YES;
            self.lastBtn = btn;
        }
        if (idx!=0&&self.showIntervalLine) {
            UIView *xline = [[UIView alloc]initWithFrame:CGRectMake(width*idx+self.leading, 10, 1, self.bounds.size.height-20)];
            xline.backgroundColor = RGBColor(237, 237, 237);
            [self.scrollView addSubview:xline];
        }

    }];
    
    if (self.showHoritonzalLine) {
        self.line = [[UIView alloc]initWithFrame:CGRectMake(self.leading, self.bounds.size.height-1, width-2*self.leading, 1)];
        _line.backgroundColor = self.selectedColor;
        [_scrollView addSubview:_line];
    }
    
    [_scrollView setContentSize:CGSizeMake(self.bounds.size.width, self.bounds.size.height)];
}
- (void)itemClick:(UIButton *)btn
{
    if (self.lastBtn&&self.lastBtn!=btn) {
        self.lastBtn.selected = NO;
    }
    btn.selected = YES;
    CGFloat width = (self.bounds.size.width-2*self.leading)/_items.count;
    NSInteger index = btn.tag - 100;
    CGRect frame = CGRectMake(2*self.leading+index*(width), self.bounds.size.height-1, width-2*self.leading, 1);
    [UIView animateWithDuration:0.3 animations:^{
        self.line.frame = frame;
    }];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(ksTabButtonView:didSelectIndex:)]) {
        [self.delegate ksTabButtonView:self didSelectIndex:index];
    }
    self.lastBtn = btn;
}
- (void)setDefaultSelectIndex:(NSInteger)defaultSelectIndex
{
    _defaultSelectIndex = defaultSelectIndex;
    UIButton *btn = [self.scrollView viewWithTag:(100+defaultSelectIndex)];
    [self itemClick:btn];
}
- (void)setCurrentSelectIndex:(NSInteger)currentSelectIndex
{
    _currentSelectIndex = currentSelectIndex;
    UIButton *selectBtn = [self.scrollView viewWithTag:100+currentSelectIndex];
    [self itemClick:selectBtn];
}
@end
